<?php
include 'public.php';

//获取总记录数
    $sql = "select count(id) num from ". PRE ."order where user_name='{$_SESSION['home']['name']}'" ;
    
    $result = mysql_query($sql);
    
    if($result && mysql_affected_rows() > 0){
        $row = mysql_fetch_assoc($result);
    }

    $total = $row['num'];       //总记录数
    //每页显示数
    $num = 10;

    //总页数
    $amount = ceil($total / $num);

    //页码
    $page = empty($_GET['p'])? 1 : (int)$_GET['p'];

    if($page < 1){
        $page = 1;
    }

    if($page > $amount){
        $page = $amount;
    }

    //偏移量
    $offset = ($page - 1) * $num;  



$sql = "select * from ".PRE."order where user_name='{$_SESSION['home']['name']}' order by addtime desc limit {$offset},{$num}";
$result = mysql_query($sql);
if($result && mysql_num_rows($result) > 0){
	while($row = mysql_fetch_assoc($result)){
		$orderlist[] = $row;
	}
}

//制作分页按钮
 $start = ($page - 3) <= 1 ? 1 : $page - 3;

    $end = ($page + 3) >= $amount ? $amount : $page + 3 ;

    for($i = $start; $i <= $end; $i++){
        if($i == $page){
            $str .= '<li><a style="color:#E50065;font-weight:bold;" href="order.php?&p='. $i .'" >['. $i .']</a></li>';
            continue;
        }
            $str .= '<li><a href="order.php?&p='. $i .'">['. $i .']</a><li>';

    }


    $nav = <<<tag
        <li><a href="order.php?p=1">首页</a></li>
        $str
        <li><a href="order.php?p=$amount">尾页</a></li>
tag;



?>
			<div class="admin_main_nav">订单管理&gt;&gt;订单列表</div>
			<div class="ucindex">
				<div class="shopcar">
					<div class="mt20">
						<table cellspacing="0" cellpadding="0" width="800">
							<tr class="color">
								<th>订单号</th>
								<th>收件人</th>
								<th>下单时间</th>
								<th width="120">订单状态</th>
								<th width="160">操作</th>
							</tr>
							<?php if(!empty($orderlist)):?>
							<?php foreach($orderlist as $val):?>
							<tr class="goods">
								<td><?php echo $val['id']?></td>
								<td><?php 
									$sql = "select name from ".PRE."order_receiver where id='{$val['rec_id']}'";
									$recuser = mysql_fetch_assoc(mysql_query($sql));
									echo $recuser['name'];
								?></td>
								<td><?php echo date('Y-m-d',$val['addtime'])?></td>
								<td><?php 
										switch($val['status']){
											case 0:
												echo '<font color="red">等待发货</font>';
												break;
											case 1:
												?>
												<div class="action">
												<div class="fl"><a class="ack" href="action.php?a=ack&id=<?php echo $val['id']?>"><div class="icon fl ack_i"></div><div class="fl">确认收货</div></a></div>
												</div>
												
												<?php
												break;
											case 2:
												echo '<font color="green">交易完成</font>';
												break;
										} 
								?></td>
								<td>
									<div class="action">
										<div class="fl"><a class="view" href="detail.php?id=<?php echo $val[id] ?>" ><div class="icon fl view_i"></div><div class="fl">详情</div></a></div>
										<?php if ($val['status'] == 0):?>
										<div class="fl"><a class="del" href="action.php?a=rm&id=<?php echo $val['id']?>"><div class="icon fl del_i"></div><div class="fl">取消</div></a></div>
										<?php else:?>
										<div class="fl"><a class="nodel"><div class="icon fl del_i"></div><div class="fl">取消</div></a></div>
										<?php endif;?>
									</div>
								</td>
							</tr>
							<?php endforeach;?>
							<?php endif;?>
						</table>
					</div>
				</div>
				<div class="searchnav">
				<div class="tips"><?php $start = $offset + 1>0? $offset+1: 0;$end = $offset + $num >= $total? $total : $offset + $num; echo "Showing $start to $end of $total entries";?></div>
				<ul class="user_nav">
					<?php echo $nav?>
				</ul>
				</div>
			</div>
	


<?php include 'public_b.php';?>
